package com.zhkj.txg.module.order.adapter

import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.order.entity.OrderListEntity
import com.zhkj.txg.utils.ImageUtils

class OrderListAdapter(type: Int = 0) :
    BaseQuickAdapter<OrderListEntity, OrderListViewHolder>(R.layout.item_order_list_shop),
    LoadMoreModule {

    var mType = type

    init {
        addChildClickViewIds(
            R.id.tvOrderListShopName,
            R.id.tvOrderListCancel,
            R.id.tvOrderListPay,
            R.id.tvOrderListShipping,
            R.id.tvOrderListConfirm,
            R.id.tvOrderListRefund,
            R.id.tvOrderListEvaluate,
            R.id.rvOrderListGoods,
            R.id.text_share,
            R.id.tvOrderListDelete,
            R.id.ivOrderListShop,
            R.id.tvOrderListWriteOffCode
        )
    }

    override fun onItemViewHolderCreated(viewHolder: OrderListViewHolder, viewType: Int) {
        val rv = viewHolder.getView<RecyclerView>(R.id.rvOrderListGoods)
        with(rv) {
            layoutManager = LinearLayoutManager(context)
            adapter = viewHolder.goodsAdapter
        }
        viewHolder.goodsAdapter.setOnItemClickListener { _, _, _ ->
            viewHolder.getView<View>(R.id.rvOrderListGoods).performClick()
        }
    }

    override fun convert(helper: OrderListViewHolder, item: OrderListEntity) {
        val imageView =
            helper.setText(R.id.tvOrderListShopName, item.shops?.shopName ?: context.getString(R.string.sell_owner))
                .setText(
                    R.id.tvOrderListStatus,
                    if (mType != 0) getGroupStatusStr(
                        item.orderStatus, item.payStatus, item.shippingStatus, item.status ?: 0, helper
                    ) else getStatusStr(item.orderStatus, item.payStatus, item.shippingStatus, helper)
                ).setGone(R.id.tvOrderListGoodsCount, mType != 0)
                .setGone(R.id.tvOrderListGoodsPrice, mType != 0)
                .setText(R.id.tvOrderListGoodsCount, "共${item.totalNumber}件，合计")
                .setText(R.id.tvOrderListGoodsPrice, buildSpannedString {
                    bold { append("￥").append(item.totalAmount ?: "0.0") }
                }).getView<ImageView>(R.id.ivOrderListShop)
        ImageUtils.loadCircleImage(context, imageView, item.shops?.shopImg ?: "")
        if (mType != 0) {
            helper.goodsAdapter.mType = mType
        }
        helper.goodsAdapter.setNewData(item.goods)
    }

    /**
     * 判断订单状态，显示对应的按钮
     */
    private fun getStatusStr(
        orderStatusStr: String,
        payStatusStr: String,
        shippingStatusStr: String,
        viewHolder: BaseViewHolder
    ): String {
        val orderStatus = orderStatusStr.toInt()
        val payStatus = payStatusStr.toInt()
        val shippingStatus = shippingStatusStr.toInt()
        // 订单已取消
        val isStatusCancel = orderStatus == 6 || orderStatus == 3
        // 收货之前并且未付款,即未付款
        val isStatusNotPay = orderStatus < 2 && payStatus == 0
        // 收货之前，已付款，未发货，即待发货
        val isStatusNotSend = payStatus == 1 && shippingStatus == 0 && orderStatus < 2
        // 收货之前，已发货，未退款，即待收货
//        val isStatusReceive = orderStatus < 2 && shippingStatus == 1 && payStatus != 3
        val isStatusReceive = orderStatus == 5
        // 已收货，未评价
        val isStatusNotCommit = orderStatus == 2
        // 已完成，已评价，未退款，即已完成
        val isStatusComplete = orderStatus == 4

        viewHolder.setGone(R.id.tvOrderListDelete, !isStatusCancel)
        viewHolder.setGone(R.id.tvOrderListPay, !isStatusNotPay)
        viewHolder.setGone(R.id.tvOrderListCancel, !isStatusNotPay)
        viewHolder.setGone(R.id.tvOrderListConfirm, !isStatusReceive)
        viewHolder.setGone(R.id.tvOrderListWriteOffCode, !isStatusReceive && !isStatusNotCommit && !isStatusComplete && !isStatusNotSend)
        viewHolder.setGone(R.id.tvOrderListRefund, isStatusCancel || isStatusNotPay || isStatusReceive)
        if (isStatusNotCommit || isStatusComplete) {
            viewHolder.setGone(R.id.tvOrderListRefund, true)
        }
        viewHolder.setTextColorRes(
            R.id.tvOrderListRefund,
            if (isStatusNotCommit) R.color.colorGradientMain else R.color.colorWhite
        )
        viewHolder.setBackgroundResource(
            R.id.tvOrderListRefund,
            if (isStatusNotCommit) R.drawable.bg_order_list_orange else R.drawable.selector_btn_color_main_round
        )
        viewHolder.setGone(R.id.tvOrderListEvaluate, true)
        return when {
            isStatusCancel -> {
                context.getString(R.string.order_status_cancel)
            }
            isStatusNotPay -> {
                context.getString(R.string.order_non_payment)
            }
            isStatusNotSend -> {
                context.getString(R.string.non_delivery)
            }
            isStatusReceive -> {
                context.getString(R.string.non_receive)
            }
            isStatusNotCommit or isStatusComplete -> {
                context.getString(R.string.order_complete)
            }
            else -> context.getString(R.string.order_status_unknown)
        }
    }

    /**
     * 判断订单状态，显示对应的按钮
     */
    private fun getGroupStatusStr(
        orderStatusStr: String,//订单状态  0=待确认,1=已确认,2=已收货,3=已取消,4=已完成
        payStatusStr: String,//支付状态  0=待支付,1=已支付,2已退款,3拒绝退款
        shippingStatusStr: String,//物流状态  0=未发货,1=已发货
        status: Int,//拼团状态  0单买 10拼单中 20拼单成功 30拼单失败
        viewHolder: BaseViewHolder
    ): String {
        val orderStatus = orderStatusStr.toInt()
        val payStatus = payStatusStr.toInt()
        val shippingStatus = shippingStatusStr.toInt()
        // 订单已取消
        val isStatusCancel = orderStatus == 3
        // 收货之前并且未付款,即未付款
        val isStatusNotPay = orderStatus < 2 && payStatus == 0
        // 收货之前，已付款，未发货，即待发货
        val isStatusNotSend = payStatus == 1 && shippingStatus == 0 && orderStatus < 2
        // 收货之前，已发货，未退款，即待收货
        val isStatusReceive = orderStatus < 2 && shippingStatus == 1 && payStatus != 3
        // 已收货，未评价
        val isStatusNotCommit = orderStatus == 2
        // 已完成，已评价，未退款，即已完成
        val isStatusComplete = orderStatus == 4

        viewHolder.setGone(R.id.tvOrderListPay, true)
            .setGone(R.id.tvOrderListCancel, true)
//            .setGone(R.id.tvOrderListConfirm,true)
//            .setGone(R.id.tvOrderListShipping,true)
            .setGone(R.id.tvOrderListRefund, true)
//            .setGone(R.id.tvOrderListEvaluate,true)
//            .setGone(R.id.tvOrderListDelete,true)

//        viewHolder.setGone(R.id.tvOrderListPay, !isStatusNotPay)//
//        viewHolder.setGone(R.id.tvOrderListCancel, !isStatusNotPay)//
        viewHolder.setGone(R.id.tvOrderListConfirm, !isStatusReceive)
        viewHolder.setGone(R.id.tvOrderListShipping, !isStatusReceive)
//        viewHolder.setGone(R.id.tvOrderListRefund, isStatusCancel || isStatusNotPay || isStatusReceive)
        viewHolder.setGone(R.id.tvOrderListEvaluate, !isStatusNotCommit)
        viewHolder.setGone(R.id.tvOrderListDelete, !isStatusCancel)
        viewHolder.setGone(R.id.text_share, true)
        return when {
            status == 30 -> {
                viewHolder.setGone(R.id.tvOrderListDelete, false)
                context.getString(R.string.group_fail)
            }
            isStatusNotPay -> {
                viewHolder.setGone(R.id.tvOrderListPay, false)
                viewHolder.setGone(R.id.tvOrderListCancel, false)
                context.getString(R.string.order_non_payment)
            }
            status == 10 -> {
                viewHolder.setGone(R.id.text_share, false)
                context.getString(R.string.group_ing)
            }
            isStatusCancel -> {
                viewHolder.setGone(R.id.tvOrderListDelete, false)
//                context.getString(R.string.group_success) + ", " + context.getString(R.string.order_status_cancel)
                context.getString(R.string.group_success)
            }
            isStatusNotSend -> {
                viewHolder.setGone(R.id.tvOrderListRefund, false)
//                context.getString(R.string.group_success) + ", " + context.getString(R.string.non_delivery)
                context.getString(R.string.group_success)
            }
            isStatusReceive -> {
//                viewHolder.setGone(R.id.tvOrderListConfirm,false)
//                viewHolder.setGone(R.id.tvOrderListShipping,false)
//                context.getString(R.string.group_success) + ", " + context.getString(R.string.non_receive)
                context.getString(R.string.group_success)
            }
            isStatusNotCommit or isStatusComplete -> {
//                context.getString(R.string.group_success) + ", " + context.getString(R.string.order_complete)
                context.getString(R.string.group_success)
            }
            else -> context.getString(R.string.order_status_unknown)
        }
    }
}

class OrderListViewHolder(view: View) : BaseViewHolder(view) {
    val goodsAdapter = OrderListGoodsAdapter()
}